package com.wjv.example.springboot.base.service;

import com.wjv.example.common.ResultInfo;
import com.wjv.example.springboot.base.dao.UserInfoDAO;
import com.wjv.example.springboot.base.dao.UserInfoRepository;
import com.wjv.example.springboot.base.entity.UserInfoEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>Title: Hello</p>
 *
 * @author wjv
 * @date Create in 2021/3/18 10:49
 * @description
 */
@Service
public class HelloService {

    private final Logger logger = LoggerFactory.getLogger(HelloService.class);

    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private UserInfoDAO userInfoDAO;

    public String hello () {
        logger.debug("debug log，变基测试。");
        logger.info("info log，变基测试");
        logger.error("error log，变基测试sss");
        // 测试 git 变基
        return "hello, springboot base";
    }

    public ResultInfo addUser() {
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setUserName(LocalDateTime.now().toString());
        userInfoEntity.setPhoneNum(LocalDateTime.now().toString());
        userInfoRepository.save(userInfoEntity);
        return ResultInfo.success(userInfoEntity);
    }

    /**
     * 测试 事务
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultInfo delUser() {
        UserInfoEntity userInfoEntity = userInfoRepository.findFirstByIdAfter(1);

        userInfoRepository.delete(userInfoEntity);

        int a = 10 / 0;

        return ResultInfo.success(null);
    }

    public ResultInfo getUser2(int id) {
       UserInfoEntity userInfoEntity = userInfoDAO.getUserInfo(id);
       return ResultInfo.success(userInfoEntity);
    }
}
